$(document).ready(function () {

    //请求是否正常制作完成
var is_error = getPageCode('is_error')
    /*var checkOrderPath = "order/stateQuery";
    var checkOrderParams = {
        SESSION_ID:SESSION_ID,
        ORDER_ID:getPageCode("order_id")
    }*/
  /*  request(checkOrderPath,checkOrderParams,function (res) {
        var flg = /^0+$/ig.test(res.RSPCD); //全零true
        if(!flg) {

        }
        var status = res.BODY.ORDER_STATE;*/
        if( is_error == "true") {
            $(".pay-success-icon").attr("src","../assets/img/error_icon.png");
            $(".success-text").text("制作出错").css("color","#ff5940");
            $(".remind-info1").text("咖啡机出现故障，你可申请退款");
            $("#machDisabled").css("display","none");
            $("#contactUs").css("display","inline-block");
            $("#start").text("申请退款");
            $("title").text("制作出错");
        }
    // })

})
$("#start").on("tap",function () {
    if($("#start").text() == "返回首页") {
        location.replace('./index.html');
    } else {
        //订单查询
        var orderId = getPageCode('order_id');
        var orderQueryPath = "order/query";
        var orderQueryParams = {
            "SESSION_ID":SESSION_ID,
            "ORDER_ID":orderId
        }
    request(orderQueryPath,orderQueryParams,function (res) {
        if(res.RSPCD == "ORDER10028") {
            location.href = "./replay_refund_error.html";
        }
        var today = getNowFormatDate();
        var orderDate = res.BODY.CREATE_TIME.split(" ")[0];
        if(today != orderDate) return showErroMsg("只能退款当天的订单!<br><span style='font-size:8px;'>订单时间：" +orderDate + "</span>",function () {
            $(".showErrorMassage").remove();
        });
        //申请退款
        var refundPath = "refund/applicate";
        var refundParams = {
            "SESSION_ID":SESSION_ID,
            "ORDER_ID":orderId,
            "SKU_ID":res.BODY.STORE_ORDER[0].SKU_LIST[0].SKU_ID,
            "REFUND_TYPE":1,
            "REFUND_FEE":res.BODY.STORE_ORDER[0].SKU_LIST[0].REFUNDABLE_FEE,
            "REASON":"制作出错",
            "MEMO":"立即退款",
            "PICS":[]
        }
        request(refundPath,refundParams,function (res) {
            if(res.RSPCD == "ORDER10001") {
                showErroMsg("订单号不存在！",function(){
                $(".showErrorMassage").remove();
            });
            } else if(res.RSPCD == "ORDER10034") {
                showErroMsg("订单未付款！",function(){
                $(".showErrorMassage").remove();
            });
            } else if(res.RSPCD == "ORDER10028") {
                showErroMsg("订单与用户不匹配！",function(){
                $(".showErrorMassage").remove();
            });
            } else if(res.RSPCD == "ORDER10027") {
                showErroMsg("商品与订单不匹配！",function(){
                $(".showErrorMassage").remove();
            });
            } else if(res.RSPCD == "ORDER10035") {
                showErroMsg("商品已退款！",function(){
                $(".showErrorMassage").remove();
            });
            } else if(res.RSPCD == "ORDER10032") {
                showErroMsg("超出可退款限额！",function(){
                $(".showErrorMassage").remove();
            });
            } else if(res.RSPCD == "ORDER10032") {
                showErroMsg("重复申请退款！",function(){
                $(".showErrorMassage").remove();
            });
            } else {
                location.replace('./report_errors_success.html');
            }
        })

    })
    }
})